/**
 * 信息窗口工具类
 * 用于生成各种类型的信息窗口内容
 */

/**
 * 根据转向角获取方向描述
 * @param yaw 转向角（弧度）
 * @returns 方向描述
 */
const getDirectionFromYaw = (yaw: number): string => {
	// 如果 yaw 已经是角度值，直接使用；如果是弧度，转换为角度
	const degrees = Math.abs(yaw) > 2 * Math.PI ? yaw : (yaw * 180) / Math.PI;

	// 将角度标准化到 0-360 度范围
	const normalizedDegrees = ((degrees % 360) + 360) % 360;

	// 根据角度范围返回方向描述
	if (normalizedDegrees >= 337.5 || normalizedDegrees < 22.5) {
		return "正北";
	} else if (normalizedDegrees >= 22.5 && normalizedDegrees < 67.5) {
		return "东北";
	} else if (normalizedDegrees >= 67.5 && normalizedDegrees < 112.5) {
		return "正东";
	} else if (normalizedDegrees >= 112.5 && normalizedDegrees < 157.5) {
		return "东南";
	} else if (normalizedDegrees >= 157.5 && normalizedDegrees < 202.5) {
		return "正南";
	} else if (normalizedDegrees >= 202.5 && normalizedDegrees < 247.5) {
		return "西南";
	} else if (normalizedDegrees >= 247.5 && normalizedDegrees < 292.5) {
		return "正西";
	} else if (normalizedDegrees >= 292.5 && normalizedDegrees < 337.5) {
		return "西北";
	}

	return "未知";
};

/**
 * 创建车辆信息窗口内容
 * @param vehicleData 车辆数据
 * @returns HTML字符串
 */
export const createVehicleInfoContent = (vehicleData: any) => {
	// 车辆状态转换
	const getVehicleStatusText = (status: number) => {
		switch (status) {
			case 0:
				return "离线";
			case 1:
				return "在线";
			case 2:
				return "行驶中";
			case 3:
				return "故障";
			default:
				return "未知";
		}
	};

	// 任务状态转换
	const getTaskStatusText = (status: number) => {
		switch (status) {
			case 0:
				return "待执行";
			case 1:
				return "执行中";
			case 2:
				return "已完成";
			case 3:
				return "已取消";
			case 4:
				return "暂停";
			case 5:
				return "异常";
			default:
				return "未知";
		}
	};

	return `
    <div style="padding: 12px; min-width: 250px; font-family: Arial, sans-serif;">
      <div style="font-size: 16px; font-weight: bold; margin-bottom: 8px; color: #333;">
        ${vehicleData?.plateNo || "未知车牌"}
      </div>
      ${
				vehicleData?.vin
					? `<div style="font-size: 12px; color: #666; margin-bottom: 6px;">
        <strong>VIN码:</strong> ${vehicleData.vin}
      </div>`
					: ""
			}
      <div style="font-size: 12px; color: #666; margin-bottom: 6px;">
        <strong>车型:</strong> ${vehicleData?.vehType || vehicleData?.vehicleType || "未知"}
      </div>
      <div style="font-size: 12px; color: #666; margin-bottom: 6px;">
        <strong>电量:</strong> ${
					vehicleData?.batteryLevel !== undefined
						? vehicleData.batteryLevel + "%"
						: vehicleData?.soc !== undefined
						? vehicleData.soc + "%"
						: "未知"
				}
      </div>
      <div style="font-size: 12px; color: #666; margin-bottom: 6px;">
        <strong>速度:</strong> ${vehicleData?.speed !== undefined ? Number(vehicleData.speed).toFixed(2) + " km/h" : "未知"}
      </div>
      <div style="font-size: 12px; color: #666; margin-bottom: 6px;">
        <strong>方向:</strong> ${vehicleData?.yaw !== undefined ? getDirectionFromYaw(vehicleData.yaw) : "未知"}
      </div>
      <div style="font-size: 12px; color: #666; margin-bottom: 6px;">
        <strong>车辆状态:</strong> 
        <span style="color: ${
					vehicleData?.vehicleStatus === 1 ? "#52c41a" : vehicleData?.vehicleStatus === 3 ? "#ff4d4f" : "#666"
				}">
          ${getVehicleStatusText(vehicleData?.vehicleStatus)}
        </span>
      </div>
      <div style="font-size: 12px; color: #666; margin-bottom: 6px;">
        <strong>任务状态:</strong> 
        <span style="color: ${vehicleData?.taskStatus === 1 ? "#1890ff" : vehicleData?.taskStatus === 2 ? "#52c41a" : "#666"}">
          ${getTaskStatusText(vehicleData?.taskStatus)}
        </span>
      </div>
      ${
				vehicleData?.anomalousStatus !== undefined && vehicleData.anomalousStatus !== 0
					? `<div style="font-size: 12px; color: #ff4d4f; margin-bottom: 6px;">
             <strong>异常状态:</strong> 异常
           </div>`
					: ""
			}
    </div>
  `;
};

/**
 * 创建标记信息窗口内容
 * @param markerData 标记数据
 * @param pointTypeList 点位类型字典
 * @returns HTML字符串
 */
export const createMarkerInfoContent = (markerData: any, pointTypeList?: any[]) => {
	// 根据字典转换类型
	const getTypeText = (typeCode: string) => {
		if (!typeCode || !pointTypeList) return "未知";
		const typeItem = pointTypeList.find((item: any) => item.value === typeCode);
		return typeItem ? typeItem.label : typeCode;
	};

	return `
    <div style="padding: 12px; min-width: 200px; font-family: Arial, sans-serif;">
      <div style="font-size: 16px; font-weight: bold; margin-bottom: 8px; color: #333;">
        ${markerData?.name || "未知名称"}
      </div>
      <div style="font-size: 12px; color: #666; margin-bottom: 6px;">
        <strong>类型:</strong> ${getTypeText(markerData?.type)}
      </div>
      <div style="font-size: 12px; color: #666; margin-bottom: 6px;">
        <strong>联系人:</strong> ${markerData?.callName || "未知"}
      </div>
      <div style="font-size: 12px; color: #666; margin-bottom: 6px;">
        <strong>联系电话:</strong> ${markerData?.callPhone || "未知"}
      </div>
    </div>
  `;
};

/**
 * 创建信息窗口配置
 * @param content 信息窗口内容
 * @param options 配置选项
 * @returns 信息窗口配置对象
 */
export const createInfoWindowConfig = (
	content: string,
	options: {
		showOnHover?: boolean;
		showOnClick?: boolean;
		offset?: [number, number];
		closeWhenClickMap?: boolean;
	} = {}
) => {
	const { showOnHover = true, showOnClick = false, offset = [0, -30], closeWhenClickMap = true } = options;

	return {
		content,
		showOnHover,
		showOnClick,
		offset,
		closeWhenClickMap
	};
};

/**
 * 创建车辆信息窗口配置
 * @param vehicleData 车辆数据
 * @param options 配置选项
 * @returns 信息窗口配置对象
 */
export const createVehicleInfoWindowConfig = (
	vehicleData: any,
	options: {
		showOnHover?: boolean;
		showOnClick?: boolean;
		offset?: [number, number];
		closeWhenClickMap?: boolean;
	} = {}
) => {
	const content = createVehicleInfoContent(vehicleData);
	return createInfoWindowConfig(content, options);
};

/**
 * 创建标记信息窗口配置
 * @param markerData 标记数据
 * @param pointTypeList 点位类型字典
 * @param options 配置选项
 * @returns 信息窗口配置对象
 */
export const createMarkerInfoWindowConfig = (
	markerData: any,
	pointTypeList?: any[],
	options: {
		showOnHover?: boolean;
		showOnClick?: boolean;
		offset?: [number, number];
		closeWhenClickMap?: boolean;
	} = {}
) => {
	const content = createMarkerInfoContent(markerData, pointTypeList);
	return createInfoWindowConfig(content, options);
};
